﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
namespace Congducsan
{
    class DBAcces
    {
        //chuỗi kết nối connect
        OleDbConnection myCon;
        public DataSet ds=new DataSet();
        DataTable myDT = new DataTable();
        public bool checkConnect = true;
        public DBAcces()
        { }
        public DBAcces(string path)
        {
            try
            {
                myCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Jet OLEDB:Database Password=123456");
                myCon.Open();
            }
            catch (Exception)
            {
                checkConnect = false;
               // MessageBox.Show("Kết nối thất bại","Thông báo");
            }
        }
        public void getDataSet(string SQLquery)
        {
            try
            {
                OleDbCommand myCom = new OleDbCommand(SQLquery, myCon);
                OleDbDataAdapter da = new OleDbDataAdapter(SQLquery, myCon);
                ds = new DataSet();
                da.Fill(ds);
                myCon.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }
        public void getDataSet(string SQLquery, string Primarykey)
        {
            try
            {
                OleDbCommand myCom = new OleDbCommand(SQLquery, myCon);
                OleDbDataAdapter da = new OleDbDataAdapter(SQLquery, myCon);
                da.Fill(ds);
                ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[Primarykey] };
                myCon.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void setQuery(string SQLquery)
        {
            try
            {
                OleDbCommand myCom = new OleDbCommand(SQLquery, myCon);
                myCom.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        public void updateData(string SQLquery)
        {
            OleDbDataAdapter da = new OleDbDataAdapter(SQLquery, myCon);
            OleDbCommandBuilder cmb = new OleDbCommandBuilder(da);
            try
            {
                da.Update(ds.Tables["Table"]);   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        public void updateDataFull(string SQLquery)
        {
            OleDbDataAdapter da = new OleDbDataAdapter(SQLquery, myCon);
            OleDbCommandBuilder cmb = new OleDbCommandBuilder(da);
            try
            {
                da.Update(ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}

